//
//  XSPInvestmentVC.m
//  Xspcf
//
//  Created by 赵欢 on 2018/1/5.
//  Copyright © 2018年 小小算盘. All rights reserved.
//

#import "XSPInvestmentVC.h"
#import "SunDelegateWebVC.h"
#import "SunLoginVC.h"
#import "SunWebVC.h"
#import "SunUseCardsVC.h"
#import "ColorButton.h"

@interface XSPInvestmentVC ()<UITextFieldDelegate>
// 标信息
@property (weak, nonatomic) IBOutlet UILabel *borrowNameLabel; // 标名称
@property (weak, nonatomic) IBOutlet UILabel *interestLable; // 预期年化利率
@property (weak, nonatomic) IBOutlet UILabel *timeLabel; // 投资期限
@property (weak, nonatomic) IBOutlet UILabel *balanceLabel; // 剩余可投金额
@property (weak, nonatomic) IBOutlet UILabel *numberLabel; // 标期数

// 投资金额
@property (weak, nonatomic) IBOutlet UITextField *investTestField; //投资金额输入框
@property (weak, nonatomic) IBOutlet UIButton *allBalanceButton; //余额全投按钮
@property (weak, nonatomic) IBOutlet UILabel *rewardLable; // 预计到期收益
@property (weak, nonatomic) IBOutlet UILabel *rewardWord;

// 卡券图标
@property (weak, nonatomic) IBOutlet UIImageView *cardImageVIew;
// 使用优惠
@property (weak, nonatomic) IBOutlet UIButton *useCardButton; //卡券按钮
@property (weak, nonatomic) IBOutlet UILabel *cardLabel; //优惠后详情

// 下方区域
@property (weak, nonatomic) IBOutlet UIButton *nextButton; // 下一步按钮
@property (weak, nonatomic) IBOutlet UIButton *checkButton; // 确认协议按钮
@property (weak, nonatomic) IBOutlet UIButton *delegateButton; // 借款协议
@property (weak, nonatomic) IBOutlet UIButton *riskButton; // 风险提示

// 支付
@property (strong, nonatomic) UIView *payView; // 支付弹窗
@property (nonatomic, strong) UILabel *money;
@property (nonatomic, strong) UIButton *choseBalanceBtn;
@property (nonatomic, strong) UILabel *balanceNum;
@property (nonatomic, strong) UILabel *bankNum;
@property (nonatomic, strong) UIButton *pay;
@property (nonatomic, strong) UILabel *cardContent;
@property (nonatomic, strong) UILabel *cardWord;


@property (nonatomic, strong) Sun *sunAlert;

// 卡券
@property (nonatomic, strong) NSDictionary *chooseCard;
@property (nonatomic, assign) NSInteger canUseCard;





@end

@implementation XSPInvestmentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"投资";
    [self investData];

    self.sunAlert = [[Sun alloc]init];
    // Do any additional setup after loading the view from its nib.
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)investData{
    self.canUseCard = [[self.borrowInfo objectForKey:@"canUseCard"]intValue];
    self.borrowNameLabel.text = [self.borrowInfo objectForKey:@"name"];
    self.numberLabel.text = [self.borrowInfo objectForKey:@"number"];
    if ([[self.borrowInfo objectForKey:@"aprAdd"] doubleValue] > 0) {
        self.interestLable.text = [NSString stringWithFormat:@"%.2f%%+%.2f%%",[[self.borrowInfo objectForKey:@"aprBase"]doubleValue],[[self.borrowInfo objectForKey:@"aprAdd"] doubleValue]];
    } else {
        self.interestLable.text = [NSString stringWithFormat:@"%.2f%%",[[self.borrowInfo objectForKey:@"aprBase"]doubleValue]];
    }

    self.timeLabel.text = [NSString stringWithFormat:@"%@%@",[self.borrowInfo objectForKey:@"days"],[self.borrowInfo objectForKey:@"time"]];
    self.balanceLabel.text = [NSString stringWithFormat:@"%@元",[Sun formatStringToMoney:[self.borrowInfo objectForKey:@"leaveMoney"]]];
    self.investTestField.text = self.investString;
    self.investTestField.delegate = self;
    if (self.investTestField.text && self.investTestField.text.length == 0) {
        self.rewardWord.text = @"可用余额";
        self.rewardLable.text = [NSString stringWithFormat:@"%.2f元",[self.balanceMoney doubleValue]];
    }else {
        self.rewardWord.text = @"预计到期收益";
        self.rewardLable.text = [NSString stringWithFormat:@"%.2f元",[self getProfit:[self.investString doubleValue]]];
    }
    
    if(self.cards.count == 0){
        [self.cardImageVIew setImage:[UIImage imageNamed:@"invest_coupong_button"]];
        [self.useCardButton setTitle:@"暂无可用>" forState:UIControlStateNormal];
        [self.useCardButton setTitleColor:[UIColor colorWithHexString:@"DFDFDF"] forState:UIControlStateNormal];
        self.useCardButton.userInteractionEnabled = NO;
    }
}
- (float)getProfit:(float)money{
    float count = [[_borrowInfo objectForKey:@"days"]intValue];
    if ([[_borrowInfo objectForKey:@"time"] isEqualToString:@"天"]) {
        count = count/30;
    }
    float x = ([[_borrowInfo objectForKey:@"aprBase"]doubleValue] + [[_borrowInfo objectForKey:@"aprAdd"]doubleValue]) / 100.0;
    float value = money * x * count / 12.0;
    float result = floor(value * 100) / 100.0;
    return result;
}
#pragma mark- 余额全投
- (IBAction)allBalanceAction:(UIButton *)sender {
    // 余额大于可投金额
    self.rewardWord.text = @"预计到期收益";
    if ([self.balanceMoney doubleValue] > [[_borrowInfo objectForKey:@"leaveMoney"] doubleValue]) {
        self.investTestField.text = [NSString stringWithFormat:@"%@",[self.borrowInfo objectForKey:@"leaveMoney"]];
        self.rewardLable.text = [NSString stringWithFormat:@"%.2f元",[self getProfit:[[_borrowInfo objectForKey:@"leaveMoney"] doubleValue]]];
    } else {
        self.investTestField.text = self.balanceMoney;
        self.rewardLable.text = [NSString stringWithFormat:@"%.2f元",[self getProfit:[self.balanceMoney doubleValue]]];
    }
}
#pragma mark- 卡券
- (IBAction)useCard:(UIButton *)sender {
    if (self.canUseCard == 0) {
        return;
    }else if (![[NSUserDefaults standardUserDefaults] boolForKey:KLOGIN]) {
        SunLoginVC *vc = [[SunLoginVC alloc]init];
        vc.isWeb = NO;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (_cards.count > 0) {
        SunUseCardsVC *vc = [[SunUseCardsVC alloc]init];
        vc.cards = _cards;
        vc.money = self.investTestField.text.doubleValue;
        int time = 0;
        if ([[_borrowInfo objectForKey:@"time"] isEqualToString:@"天"]) {
            time = [[_borrowInfo objectForKey:@"days"] intValue];
        }else {
            time = [[_borrowInfo objectForKey:@"days"] intValue] * 30;
        }
        vc.days = time;
        [vc chooseCards:^(NSDictionary *cradDic) {
            _chooseCard = cradDic;
            [self loadCard];

        }];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)loadCard{
    if (self.chooseCard != nil){
        if ([[_chooseCard objectForKey:@"type"]intValue] == 1) {
            self.cardWord.text = @"使用返现券";
            self.cardContent.text = [NSString stringWithFormat:@"%d元",[[_chooseCard objectForKey:@"money"]intValue]];
            [self.useCardButton setTitle:[NSString stringWithFormat:@"%d元返现券>",[[_chooseCard objectForKey:@"money"]intValue]] forState:UIControlStateNormal];
            NSString *cardStr = [NSString stringWithFormat:@"使用后返现%d元到账户余额",[[_chooseCard objectForKey:@"money"]intValue]];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:cardStr];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"A2A2A2"] range:NSMakeRange(0,4)];
            NSRange dao = [cardStr rangeOfString:@"到账户余额"];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"FFA887"] range:NSMakeRange(5,dao.location)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"A2A2A2"] range:dao];
            [self.cardLabel setAttributedText:str];
        }else {
            self.cardWord.text = @"使用加息券";
            self.cardContent.text = [NSString stringWithFormat:@"%.1f%%",[[_chooseCard objectForKey:@"apr"]doubleValue]];
            [self.useCardButton setTitle:[NSString stringWithFormat:@"%.1f%%加息券>",[[_chooseCard objectForKey:@"apr"]doubleValue]] forState:UIControlStateNormal];
            int time = 0;
            if ([[_borrowInfo objectForKey:@"time"] isEqualToString:@"天"]) {
                time = [[_borrowInfo objectForKey:@"days"] intValue];
            }else {
                time = [[_borrowInfo objectForKey:@"days"] intValue] * 30;
            }
            NSString *cardStr = [NSString stringWithFormat:@"加息券收益%.2f元",[[_chooseCard objectForKey:@"apr"]doubleValue]/100 * self.investTestField.text.doubleValue * time/360];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:cardStr];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"A2A2A2"] range:NSMakeRange(0,4)];
            NSRange dao = [cardStr rangeOfString:[NSString stringWithFormat:@"%.2f元",[[_chooseCard objectForKey:@"apr"]doubleValue]/100 * self.investTestField.text.doubleValue * time/360]];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"FFA887"] range:dao];
            [self.cardLabel setAttributedText:str];
        }
    }else {
        self.cardWord.text = @"未使用优惠券";
        self.cardContent.text = @"-";
        self.cardLabel.text = @"未使用优惠券";
        [self.useCardButton setTitle:[NSString stringWithFormat:@"%d张卡券>",(int)_cards.count] forState:UIControlStateNormal];
    }
}
#pragma mark- 下一步
- (IBAction)nextAxtion:(UIButton *)sender {
    [self gotoPay];
}
#pragma mark- 确认
- (IBAction)checkAction:(UIButton *)sender {
    sender.selected = !sender.selected;
}
#pragma mark- 协议
- (IBAction)delegateAction:(UIButton *)sender {
    SunWebVC *vc = [[SunWebVC alloc]init];
    NSString *str = @"CZfSDQzsuDayjfzE";
    NSString *str1 =@"12345678";
    NSString *token_id=[Utility encryptStr:[NSString stringWithFormat:@"%@",[Sun getUser:ACCESSID]]  Key:str1 IV:nil];
    NSString *md5Str=[Utility md5:[NSString stringWithFormat:@"borrowId=%@&source=ios&token_id=%@&%@",self.borrowId,token_id,str]];
    vc.urlStr = [NSString stringWithFormat:@"%@/Invest/agreement?source=ios&token_id=%@&borrowId=%@&sign=%@",KWAP,token_id,self.borrowId,md5Str];
    vc.titleName = @"借款协议书";
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark- 风险
- (IBAction)riskAction:(UIButton *)sender {
    SunWebVC *vc = [[SunWebVC alloc]init];
    // http://localhost/topic/risk_warning 风险提示
    vc.urlStr = [NSString stringWithFormat:@"%@/topic/risk_warning?source=ios",KWAP];
    vc.titleName = @"风险提示";
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark- textFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < string.length) {
        NSString * str = [string substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [str rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    
    NSString *result = self.investTestField.text;
    if (string.length > 0) {
        result = [result stringByAppendingString:string];
    }else if(result.length > 0 && result.length > 0){
        result = [result substringToIndex:result.length - 1];
    }
    self.rewardWord.text = @"预计到期收益";
    self.rewardLable.text = [NSString stringWithFormat:@"%.2f元",[self getProfit:result.doubleValue]];
    if (result.doubleValue > [[_borrowInfo objectForKey:@"leaveMoney"] doubleValue]) {
        NSString *s = [_borrowInfo objectForKey:@"leaveMoney"];
        s = [s isKindOfClass:[NSNumber class]]?[NSString stringWithFormat:@"%@",s]:s;
        result = s;
        textField.text = [NSString stringWithFormat:@"%d",result.intValue];
        self.rewardLable.text = [NSString stringWithFormat:@"%.2f元",[self getProfit:result.doubleValue]];
        if (string.length > 0) {
            res = NO;
        }
    }
    return res;
}

#pragma mark- Pay
- (void)gotoPay{
    if (!self.checkButton.selected) {
        [[Sun shareSun]showMsg:@"请确认借款协议和风险提示"];
        return;
    }
    NSString *start = [Sun formatStringToMoney:[_borrowInfo objectForKey:@"limitMoney"]]; // 起投金额
    NSDictionary *dic = [Sun getUser:USER];
    if (![[NSUserDefaults standardUserDefaults] boolForKey:KLOGIN]) {
        SunLoginVC *vc = [[SunLoginVC alloc]init];
        vc.isWeb = NO;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }else if ([[dic objectForKey:@"status"] intValue] != 3) {
        [SunSupport shouldAlert:self code:UserOpen];
        return;
    }else if ([[dic objectForKey:@"status"] intValue] != 3) {
        [SunSupport shouldAlert:self code:UserBind];
        return;
    }else if ([[dic objectForKey:@"isActivate"] intValue] == 1) {
        [SunSupport shouldAlert:self code:UserActivate];
    }else{
        
        if (self.investTestField.text && self.investTestField.text.length == 0) {
            [[Sun shareSun]showMsg:@"请输入投资金额"];
            return;
        }else if (self.investTestField.text.doubleValue < start.doubleValue){
            [[Sun shareSun]showMsg:@"投资金额应大于起投金额"];
            return;
        }else if (self.investTestField.text.doubleValue > [[dic objectForKey:@"balanceMoney"] doubleValue]){
            [self setBalanceBankValueNormal];
            [self creatPayView];
            return;
        }else if (_chooseCard){
            if (self.investTestField.text.doubleValue < [[_chooseCard objectForKey:@"limitMoney"]doubleValue]) {
                [[Sun shareSun]showMsg:@"投资金额应大于卡券的起投金额"];
                return;
            }else if ([[_chooseCard objectForKey:@"limitDay"]intValue] > 0) {
                int time;
                if ([[_borrowInfo objectForKey:@"time"] isEqualToString:@"天"]) {
                    time = [[_borrowInfo objectForKey:@"days"] intValue];
                }else {
                    time = [[_borrowInfo objectForKey:@"days"] intValue] * 30;
                }
                if (time < [[_chooseCard objectForKey:@"limitDay"]intValue]) {
                    [[Sun shareSun]showMsg:[NSString stringWithFormat:@"该卡券限%@天及以上产品使用",[_chooseCard objectForKey:@"limitDay"]]];
                    return;
                }
            }
        }
    }
    [self setBalanceBankValueNormal];
    [self creatPayView];
}
#pragma mark- creatPayView
- (void)creatPayView{
    NSDictionary *dic = [Sun getUser:USER];
    if (!self.payView) {
        self.payView = [[UIView alloc]init];
        [self.payView lt_setSize:CGSizeMake(300, 300) save:YES];
        self.payView.backgroundColor = WHITE;
        self.payView.layer.cornerRadius = 5;
        self.payView.clipsToBounds = YES;
        
        UIButton *cancel = [[UIButton alloc]init];
        [self.payView addSubview:cancel];
        [cancel lt_setTop:Y(15) to:nil save:YES];
        [cancel lt_setLeft:X(15) to:nil save:YES];
        [cancel lt_setSize:CGSizeMake(12, 12) save:YES];
        [cancel lt_layoutView:cancel side:SunCenterX to:nil toSide:SunCenterX withSize:1 offset:X(-144) save:YES];
        [cancel setImage:[UIImage imageNamed:@"tool_cancel"] forState:UIControlStateNormal];
        [cancel addTarget:self action:@selector(hideAlert) forControlEvents:UIControlEventTouchUpInside];
        // 投资金额-文字
        UILabel *moneyWord = [[UILabel alloc]init];
        [self.payView addSubview:moneyWord];
        moneyWord.text = @"投资金额";
        [moneyWord setFont:[UIFont systemFontOfSize:14]];
        [moneyWord setTextColor:[UIColor colorWithHexString:@"A5A5A5"]];
        [moneyWord lt_XcenterTo:nil save:YES];
        [moneyWord lt_setTop:15 to:nil save:YES];
        // 金额-数字
        self.money = [[UILabel alloc]init];
        [self.payView addSubview:self.money];
        [self.money setMarkup:[NSString stringWithFormat:@"￥%@",[Sun formatStringToMoney:self.investTestField.text]]];
//        [self.money setFont:[UIFont systemFontOfSize:36]];
        self.money.font = [UIFont fontWithName:@"PingFangSC-Medium" size:36];
        [self.money setTextColor:[UIColor colorWithHexString:@"D0021B"]];
        [self.money lt_XcenterTo:nil save:YES];
        [self.money lt_setTop:2 to:moneyWord save:YES];

        
        // 余额支付前按钮
        self.choseBalanceBtn = [[UIButton alloc]init];
        [self.choseBalanceBtn setFrame:CGRectMake(28, 106, 17, 17)];
        [self.choseBalanceBtn setImage:[UIImage imageNamed:@"chose_normal"] forState:UIControlStateNormal];
        [self.choseBalanceBtn setImage:[UIImage imageNamed:@"chose_highlight"] forState:UIControlStateSelected];
        NSString *balanceMoney = [dic objectForKey:@"balanceMoney"];
        if (balanceMoney.doubleValue == 0) {
            self.choseBalanceBtn.selected = NO;
        }else{
            self.choseBalanceBtn.selected = YES;
        }
        //        [choseBalanceBtn setUserInteractionEnabled:NO];
        //是否使用余额
        [self.choseBalanceBtn addTarget:self action:@selector(choseBalanceBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_payView addSubview:self.choseBalanceBtn];
        // 余额支付文字
        UILabel *balanceWord = [[UILabel alloc]init];
        [balanceWord setText:@"使用可用余额支付"];
        [balanceWord setFont:[UIFont systemFontOfSize:12]];
        [balanceWord setTextColor:[UIColor colorWithHexString:@"808080"]];
        [balanceWord setFrame:CGRectMake(52, 106, 150, 20)];
        [self.payView addSubview:balanceWord];
        // 余额数字
        self.balanceNum = [[UILabel alloc]init];
        self.balanceNum.textAlignment = UITextAlignmentRight;
        [self.balanceNum setFont:[UIFont systemFontOfSize:12]];
        [self.balanceNum setTextColor:[UIColor colorWithHexString:@"FFAD8D"]];
        [self.balanceNum setFrame:CGRectMake(X(160), 106, X(150), 20)];
        [self.payView addSubview:self.balanceNum];
        
        // 银行卡数字
        self.cardContent = [[UILabel alloc]init];
        self.cardContent.textAlignment = UITextAlignmentRight;
        [self.cardContent setFont:[UIFont systemFontOfSize:12]];
        [self.cardContent setTextColor:[UIColor colorWithHexString:@"FFAD8D"]];
        [self.cardContent setFrame:CGRectMake(X(160), 128, X(150), 20)];
        [self.payView addSubview:self.cardContent];
        
        // 需银行卡支付文字
        self.cardWord = [[UILabel alloc]init];
        [self.cardWord setText:@"未使用优惠券"];
        [self.cardWord setFont:[UIFont systemFontOfSize:12]];
        [self.cardWord setTextColor:[UIColor colorWithHexString:@"808080"]];
        [self.cardWord setFrame:CGRectMake(52, 128, 100, 20)];
        [self.payView addSubview:self.cardWord];
        
        
        // 银行卡数字
        self.bankNum = [[UILabel alloc]init];
        self.bankNum.textAlignment = UITextAlignmentRight;
        [self.bankNum setFont:[UIFont systemFontOfSize:12]];
        [self.bankNum setTextColor:[UIColor colorWithHexString:@"FFAD8D"]];
        [self.bankNum setFrame:CGRectMake(X(160), 150, X(150), 20)];
        [self.payView addSubview:self.bankNum];
        
        // 需银行卡支付文字
        UILabel *bankWord = [[UILabel alloc]init];
        [bankWord setText:@"需银行卡支付"];
        [bankWord setFont:[UIFont systemFontOfSize:12]];
        [bankWord setTextColor:[UIColor colorWithHexString:@"808080"]];
        [bankWord setFrame:CGRectMake(52, 150, 100, 20)];
        [self.payView addSubview:bankWord];

        // 前往支付红色按钮
        self.pay = [[UIButton alloc]init];
        [_payView addSubview:self.pay];
        [self.pay lt_setTop:Y(150) to:moneyWord save:YES];
        [self.pay lt_setSize:CGSizeMake(X(285), Y(44)) save:YES];
        [self.pay lt_setBottom:Y(22) to:nil save:YES];
        [self.pay lt_XcenterTo:nil save:YES];
        self.pay.clipsToBounds = YES;
//        [self.pay setBackgroundColor:[UIColor colorWithRed:248/255.0 green:59/255.0 blue:68/255.0 alpha:1] forState:UIControlStateNormal];
        [self.pay setBackgroundImage:[UIImage imageNamed:@"button_alert"] forState:UIControlStateNormal];
        self.pay.layer.cornerRadius = 5;
        [self.pay setTitleColor:WHITE forState:UIControlStateNormal];
        self.pay.titleLabel.font = [UIFont systemFontOfSize:18];
        [self.pay addTarget:self action:@selector(payAction) forControlEvents:UIControlEventTouchUpInside];
        [self setBalanceBankValueNormal];
        
    }
//    [self.view addSubview:self.payView];
    [self.sunAlert show:self.payView to:self shouldReturn:YES];
}
- (void)setBalanceBankValueNormal{
    [self.money setText:self.investTestField.text];
    self.choseBalanceBtn.selected = YES;
    NSDictionary *dic = [Sun getUser:USER];
    NSString *balanceMoney = [dic objectForKey:@"balanceMoney"];
    CGFloat balanceNumValue = balanceMoney.doubleValue;
    CGFloat bankNumValue = self.investTestField.text.doubleValue - balanceMoney.doubleValue;
    if (balanceMoney.doubleValue >= self.investTestField.text.doubleValue) {
        balanceNumValue = self.investTestField.text.doubleValue;
        bankNumValue = 0;
    }
    [self.balanceNum setText:[NSString stringWithFormat:@"%.2f元",balanceNumValue]];
    [self.bankNum setText:[NSString stringWithFormat:@"%.2f元",bankNumValue]];
    if (bankNumValue>=0) {
        [self.pay setTitle:@"前往支付" forState:UIControlStateNormal];
    }else{
        [self.pay setTitle:@"立即投资" forState:UIControlStateNormal];
    }
}
#pragma mark- 是否选择余额
- (void)choseBalanceBtnAction:(UIButton*)button{
    button.selected = !button.selected;
    if (!button.selected) {
        [self.balanceNum setText:[NSString stringWithFormat:@"0.00元"]];
        [self.bankNum setText:[NSString stringWithFormat:@"%.2f元",self.investTestField.text.doubleValue]];
    }else{
        [self setBalanceBankValueNormal];
    }
}
- (void)payAction {
    [[Sun shareSun]showActivity:self];
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_TENDER];
    NSDictionary *params;
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if (self.choseBalanceBtn.selected) {
        // 使用余额
        params = @{@"accessId":[Sun getUser:ACCESSID],@"money":self.investTestField.text,@"borrowId":self.borrowId,@"cardId":_chooseCard ? [self.chooseCard objectForKey:@"cardId"]:@"",@"device":@"iOS",@"cardType":_chooseCard?[_chooseCard objectForKey:@"type"]:@"",@"useBalance":@"1",@"idfa":[user objectForKey:@"ADID"]};
    }else{
        // 不使用余额
        params = @{@"accessId":[Sun getUser:ACCESSID],@"money":self.investTestField.text,@"borrowId":self.borrowId,@"cardId":_chooseCard ? [self.chooseCard objectForKey:@"cardId"]:@"",@"device":@"iOS",@"cardType":_chooseCard?[_chooseCard objectForKey:@"type"]:@"",@"useBalance":@"0",@"idfa":[user objectForKey:@"ADID"]};
    }
    
    // 加密
    NSString *str = [Sun stringFromDictionary:(NSDictionary *)params];
    // 界面
    SunDelegateWebVC *vc = [[SunDelegateWebVC alloc]init];
    vc.params = str;
    vc.urlStr = url;
    vc.title = @"投标";
    vc.type = MakeVerb;
    [self.navigationController pushViewController:vc animated:YES];
    [self hideAlert];
}
- (void)hideAlert{
    [self.sunAlert hideAll];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
